var CompanyMaterialTable = function (companyMaterialTable) {
  //继承BaseModel
  BaseModel.call(this);
  var self = this;
  var imgFields = ['img', 'imgTwo', 'imgThree', 'imgFour'];
  var copyFields = ['id', 'materialCode', 'materialName', 'model', 'providerName', 'color', 'colorDescription', 'materialType', 'status', 'isStock',
    'isEquals', 'unit', 'unitPrice', 'defaultWarehouse', 'safetyStockMin', 'safetyStockMax', 'procurementCycles', 'transmittance', 'material',
    'count', 'creater', 'creationTime', 'lastModifier', 'lastModifyTime', 'purchaseApproval', 'providerNo', 'materialDept'
  ];
  var fn = {
    initData: function (companyMaterialTable) {
      if (companyMaterialTable === undefined) {
        companyMaterialTable = {};
      }
      self.setDataFromObj(companyMaterialTable, copyFields, '');
      if (SysUtil.isEmptyObjct(companyMaterialTable)) {
        self.isStock = 1;
        self.isEquals = 0;
        self.unitPrice = undefined;
        self.safetyStockMax = undefined;
        self.safetyStockMin = undefined;
        self.procurementCycles = undefined;
        self.status = "使用";
        self.defaultWarehouse = "原材料仓";
        self.imgUrls = [];
      } else {
        var imgUrls = [];
        for (var i = 0; i < imgFields.length; i++) {
          var imgField = imgFields[i];
          var imgUrl = companyMaterialTable[imgField];
          if (!SysUtil.isEmpty(imgUrl)) {
            imgUrls.push(imgUrl);
          }
        }
        self.imgUrls = imgUrls;
      }
    },

    getErrorMsg: function (code) {
      var errorObj = {
        materialName: "请输入物料名称",
        model: "请输入规格型号",
        providerName: "请选择供应商名称",
        materialType: "请选择物料类型",
        unit: "请选择单位"
      };
      return errorObj[code];
    }
  };

  this.checkValid = function () {
    var requiredFields = ['materialName', 'model', 'providerName', 'unit'];
    if (!CheckVaildUtil.checkNullValue(requiredFields, this, fn.getErrorMsg)) {
      return false;
    }

    var $alert = Vue.prototype.$alert;
    if (this.safetyStockMax < this.safetyStockMin) {
      $alert('库存上限不能小于库存下限', '信息');
      return false;
    }
    return true;
  };

  this.getObject = function (type) {
    var postData = {};

    var id = this.id;
    if (id !== '') {
      postData.id = id;
    }

    if (this.color === null) {
      postData.color = '';
    }
    this.setPostData(postData, copyFields, true, true);

    var materialDept = postData.materialDept;
    if (!SysUtil.isEmpty(materialDept)) {
      postData.materialDeptId = materialDept.materialDeptId;
    }
    delete postData.materialDept;

    var imgUrls = this.imgUrls;
    for (var i = 0; i < imgFields.length; i++) {
      var imgField = imgFields[i];
      var imgUrl = imgUrls[i];
      if (SysUtil.isEmpty(imgUrl)) {
        imgUrl = '';
      }
      postData[imgField] = imgUrl;
    }

    return postData;
  };

  fn.initData(companyMaterialTable);
};
